﻿/* ================================================================================
<copyright file="Program.cs" company="The Framework Factory, LLC" lastWriteTime="12/05/2012 23:23:46 PM">
  Copyright (c) 2012 The Framework Factory, LLC.  All rights reserved.
</copyright>
=================================================================================== */
using System;
using System.Threading;

using FWF.Logging.Net;

namespace FWF.Logging.ConsoleTestAppWithNet
{
    /// <summary>
    /// 
    /// </summary>
    class Program
    {

        private static readonly ManualResetEvent _resetEvent = new ManualResetEvent( false );

        private static readonly Random _random = new Random();

        private static readonly IFWFLog _log = LogFactory.Get( typeof( Program ) );

        /// <summary>
        /// Mains the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        static void Main( string[] args )
        {
            // Start the publisher to send log events to any subscribers
            LogPublisher.RegisterAndListen();

            // When the cancel key is pressed, then reset the event component
            Console.CancelKeyPress +=
                ( sender, e ) =>
                {
                    _resetEvent.Set();
                };

            // 
            GenerateRandomLogEntries();

            Console.WriteLine("Done!");
        }

        /// <summary>
        /// Generates the random log entries.
        /// </summary>
        private static void GenerateRandomLogEntries()
        {
            while ( true )
            {
                int x = _random.Next( 1, 1000 );

                if ( x < 333 )
                {
                    _log.Debug( "This is a debug statement..." );
                }
                else if ( x < 666 )
                {
                    _log.Warn( "This is a warn statement..." );
                }
                else
                {
                    _log.Error(
                        "This is a error statement...",
                        new Exception( "exception message" )
                        );
                }

                bool signaled = _resetEvent.WaitOne( TimeSpan.FromMilliseconds( 500 ) );

                if ( signaled )
                {
                    break;
                }
            }
        }

    }
}
